#if 0
/*
	/bin/kill -L
*/
#define _GNU_SOURCE // 如是不声明gettid()不识别报错
#include <sys/types.h>
#include <unistd.h>

#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void handle(int);

int main(void) {
	signal(SIGINT, handle);
	signal(SIGTERM, handle);
	while (1) {
		printf("test\n");
		sleep(1);
	}
}
void handle(int signal) {
	gettid();
	if (signal == SIGINT) {
		printf("Ctrl + C\n");
	} else if (signal == SIGTERM) {
		printf("kill\n");
	}
	exit(EXIT_SUCCESS);
}
#endif

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct {
	char *message;
	int read_count;
} return_type;

void *thread_a(void *pv);
// void *thread_b(void *pv);

int main(void) {
	pthread_t a;
	pthread_t b;
	// pthread_attr_t _pthread_attr;
	pthread_create(&a, NULL, thread_a, "eof");
	// pthread_create(&b, NULL, thread_b, "EOF");
	return_type *p = NULL;
	pthread_join(a, (void **)p);
	return_type ra = *p;
	if (p) {
		free(p);
		p = NULL;
	}
#if 0
	pthread_join(b, (void **)p);
	return_type rb = *p;
	if (p) {
		free(p);
		p = NULL;
	}
#endif
	return 0;
}

void *thread_a(void *pv) {
	printf("线程A 输入%s结束\n", (char *)pv);
	char buffer[1024];
	ssize_t read_size = 0;
	int count = 0;
	while ((read_size = read(STDIN_FILENO, buffer, 1024)) > 0) {
		// 读取到换行
		buffer[read_size - 1] = '\0';
		if ((read_size == strlen((char *)pv)) && !strcmp((char *)pv, buffer)) {
			return_type *r = (return_type *)malloc(1 * sizeof(return_type));
			r->message = "线程A退出";
			r->read_count = count;
			// pthread_exit(&r);
			return &r;
		}
		printf("线程A读取到输入：\n");
		fflush(stdout);
		write(STDOUT_FILENO, buffer, read_size);
		count += read_size;
	}
}
